﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class TblKhachHangDAO:IData<TblKhachHang> 
 	{
		public TblKhachHangDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref TblKhachHang item, int t) {
			 return connec.ExecuteNonQuery("sp_TblKhachHang", CommandType.StoredProcedure,new SqlParameter("@flag", t),
				 new SqlParameter("@" + TblKhachHang.c_MaKhachHang, item.MaKhachHang), new SqlParameter("@" + TblKhachHang.c_TenKhachHang, item.TenKhachHang),
				 new SqlParameter("@" + TblKhachHang.c_MaDonVi, item.MaDonVi), new SqlParameter("@" + TblKhachHang.c_DaXoa, item.DaXoa));
		}

		public override TblKhachHang GetItem(IDataReader dr)
		{
			TblKhachHang obj = new TblKhachHang();
			obj.MaKhachHang = Convert.ToInt32(dr[TblKhachHang.c_MaKhachHang]);
			obj.TenKhachHang = Convert.ToString(dr[TblKhachHang.c_TenKhachHang]);
			obj.MaDonVi = Convert.ToInt32(dr[TblKhachHang.c_MaDonVi]);
			obj.DaXoa = Convert.ToBoolean(dr[TblKhachHang.c_DaXoa]);
            obj.TenDonVi = Convert.ToString(dr[TblKhachHang.c_TenDonVi]);
			return obj;
		}

		public override List<TblKhachHang> GetAll()
		{
			return GetAll("sp_GetAllTblKhachHang", CommandType.StoredProcedure);
		}

		public override TblKhachHang GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.MaKhachHang == Convert.ToInt32(id));
		}
		public override TblKhachHang GetItemById(TblKhachHang item){
			throw new NotImplementedException();
		}

	}

 }
